const userModel = require('../models/user')

//查询用户列表
const queryUserList = async (ctx, next) => {
  //console.log(ctx)
  let result = await userModel.findUserList()
  if(result.status === 1){
    ctx.body = {
      code: 200,
      data: result.data
    }
  }else{
    ctx.body = {
      code: 500,
      message: '查询数据失败'
    }
  }
}

//删除用户
const deleteUser = async (ctx, next) => {
    let id = ctx.request.body.id
    let result = await userModel.handleDeleteUser(id)
    if(result.status === 1){
      ctx.body = {
        code: 200,
        message: result.message
      }
    }else{
      ctx.body = {
        code: 500,
        message: result.message
      }
    }
}


//增加用户
const addUser = async (ctx, next) => {
    let params = ctx.request.body
    let result = await userModel.handleAddUser(params)
    if(result.status === 1){
      ctx.body = {
        code: 200,
        message: result.message
      }
    }else{
      ctx.body = {
        code: 500,
        message: result.message
      }
    }
}


//修改用户
const updateUser = async (ctx, next) => {
  const result = await userModel.handleUpdateUser(ctx.request.body)
  if(result.status === 1){
    ctx.body = {
      code: 200,
      message: '修改成功'
    }
  }else{
    ctx.body = {
      code: 500,
      message: '修改失败'
    }
  }
}


module.exports = {
  queryUserList,
  deleteUser,
  addUser,
  updateUser
}